//
//  InfoDetail.m
//  Trading365
//
//  Created by Jason on 12-2-3.
//  Copyright (c) 2012年 dhc. All rights reserved.
//

#import "ChanceInfo.h"
#import "InfoType.h"
#import "TaskManager.h"
#import "XmlRpc.h"
#import "InfoImageCell.h"
#import "CustomerInfoDetail.h"
#import "CustomerContactList.h"
#import "CustomerPictList.h"
#import "DHCUtil.h"

@implementation ChanceInfo
@synthesize table;
@synthesize key_name;
@synthesize key_id;
@synthesize InfoListTitle,InfoListName;
@synthesize responseParam;

@synthesize employeeName;

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title=self.key_name;
    [self.table setBackgroundColor:BACKGROUND_COLOR];

    NSArray *temp_array=[[NSArray alloc]initWithObjects: @"商机名称",@"客户",@"营销方式",@"发现时间",@"状态",@"所属业务员",@"创建时间",@"客户需求",nil];
    self.InfoListTitle=temp_array;
    [temp_array release];
    
    NSArray *temp_array2=[[NSArray alloc]initWithObjects:@"chance_name",@"customer_name_cn",@"dict_name_cn",@"find_date",@"conf_dictionary_fix_name",@"user_name_cn",@"create_time",@"customer_require", nil];
    self.InfoListName=temp_array2;
    [temp_array2 release];

    [self refreshData];
    [self.table reloadData];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    
    [[[TaskManager Share] GetTaskByType:GetChanceInfo Delegate:self] cancel];
}

- (void)dealloc {
    [responseParam release];
    
    self.key_name=nil;
    self.key_id=nil;
    self.InfoListTitle=nil;
    self.InfoListName = nil;
    
    [table release];
    [super dealloc];
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.InfoListTitle count];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell...
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView.backgroundColor = [UIColor clearColor];
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    
    NSUInteger row=indexPath.row;
    if (row < [self.InfoListTitle count]) 
    {
        cell.textLabel.text = [self.InfoListTitle objectAtIndex:row];
    }
    
    if (row < [self.InfoListName count]) 
    {
        cell.detailTextLabel.text =[responseParam objectForKey:[self.InfoListName objectAtIndex:row]];
        //format date
        if (row == 6)
        {
            cell.detailTextLabel.text =[responseParam objectForKey:[self.InfoListName objectAtIndex:row]];
        }
    }


    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.  
}

#pragma mark - 
#pragma mark - my func

-(void) OnPreExcute
{
    [super OnPreExcute];
}

-(void) DoInBackground
{
    XmlRpc *xml = [[XmlRpc alloc] init];
    NSMutableDictionary *tmp=nil;

    tmp = [xml GetChance:self.key_id];
    
    responseParam = [[NSMutableDictionary alloc] initWithDictionary:tmp];
    
    [xml release];
    [self.table reloadData];
}

-(void) OnPostExcute
{
    [super OnPostExcute];
}

-(void)refreshData
{    
    [[[TaskManager Share] GetTaskByType:GetChanceInfo Delegate:self] Excute];
}

@end
